package com.heer.gymnasium.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.ContextThemeWrapper;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.constant.Extras;
import com.heer.model.BookOrderBean;
import com.squareup.timessquare.CalendarCellView;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.DefaultDayViewAdapter;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by rzw2 on 2017/4/8.
 */

public class SiteReservedFastActivity extends BaseActivity {
    @BindView(R.id.image_site_status)
    ImageView imageSiteStatus;
    @BindView(R.id.calendar_view)
    CalendarPickerView calendarView;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;

    List<BookOrderBean> list;
    List<String> badDays;

    public static void toReservedFast(Context context, String site_id, Serializable times, Serializable badDays,String isauto) {
        Intent intent = new Intent();
        intent.setClass(context, SiteReservedFastActivity.class);
        intent.putExtra(Extras.EXTRA_ID, site_id);
        intent.putExtra(Extras.EXTRA_TIME, times);
        intent.putExtra(Extras.EXTRA_DATA, badDays);
        intent.putExtra("isauto", isauto);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_site_reserved_fast);
        badDays = (List<String>) getIntent().getSerializableExtra(Extras.EXTRA_DATA);
    }

    @Override
    protected void initView() {
        super.initView();
        initCalendar();
        titleBuilder.setTitleText("快捷预订");
        list = (List<BookOrderBean>) getIntent().getSerializableExtra(Extras.EXTRA_TIME);
    }

    @Override
    protected void loadData() {
        super.loadData();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (calendarView.getAdapter().getCount() != 1) {
                    ViewGroup.LayoutParams params = calendarView.getLayoutParams();
                    params.height = calendarView.getHeight() * 2;
                    calendarView.setLayoutParams(params);
                }
            }
        }, 500);

    }

    @OnClick(R.id.btn_submit)
    public void onClick() {
        List<BookOrderBean> data = new ArrayList<>();
        if (calendarView.getSelectedDates().size() > 0 && list.size() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (Date date : calendarView.getSelectedDates()) {
                for (BookOrderBean item : list) {
                    BookOrderBean nit = new BookOrderBean();
                    nit.setBegin(item.getBegin());
                    nit.setDay(sdf.format(date));
                    nit.setEnd(item.getEnd());
                    nit.setName(item.getName());
                    nit.setNum(item.getNum());
                    nit.setPrice(item.getPrice());
                    nit.setSite_name(item.getSite_name());
                    nit.setStatus(item.getStatus());
                    nit.setTid(item.getTid());
                    data.add(nit);
                }
            }
        }

        SiteReservedInfoActivity.toReservedInfo(mContext, getIntent().getStringExtra(Extras.EXTRA_ID), (Serializable) data,
                getIntent().getStringExtra("isauto"));
    }

    private void initCalendar() {
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.DATE, 30);
        Date today = new Date();
        calendarView.setDateSelectableFilter(new CalendarPickerView.DateSelectableFilter() {
            @Override
            public boolean isDateSelectable(Date date) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dateStr = sdf.format(date);
                for (String d : badDays) {
                    if (d.equals(dateStr)) {
                        return false;
                    }
                }
                return true;
            }
        });
        calendarView.setCustomDayView(new DefaultDayViewAdapter() {
            @Override
            public void makeCellView(CalendarCellView parent) {
                TextView textView = new TextView(
                        new ContextThemeWrapper(parent.getContext(), R.style.CalendarCell_CalendarDate));
                textView.setDuplicateParentStateEnabled(true);
                parent.addView(textView);
                parent.setDayOfMonthTextView(textView);
            }
        });
        calendarView.init(today, nextYear.getTime())
                .inMode(CalendarPickerView.SelectionMode.MULTIPLE)
                .withSelectedDate(today);
    }
}
